package com.koi.clubservice.Utils;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class CourseScheduleUtils {
    // 学期开始日期
    private static final String START_DATE = "2025-02-24";
    // 每大节课的时间范围
    private static final String[][] CLASS_TIME_RANGES = {
            {"08:20", "09:50"},
            {"10:15", "11:45"},
            {"13:50", "15:20"},
            {"15:40", "17:10"},
            {"17:45", "19:15"},
            {"19:30", "21:00"}
    };

    public static List<DateRange> convertToTime(String week, String period) {
        List<DateRange> dateRanges = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        // 解析周次
        List<Integer> weeks = parseWeeks(week);
        // 解析节次
        int[] classInfo = parseClassPeriod(period);
        int dayOfWeek = classInfo[0];
        int startClass = classInfo[1];
        int endClass = classInfo[2];
        boolean isSingleWeek = classInfo[3] == 1;
        boolean isDoubleWeek = classInfo[4] == 1;

        try {
            Date startDate = dateFormat.parse(START_DATE + " 00:00");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);

            // 计算学期开始日是星期几
            int startDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            int daysToFirstMonday = (Calendar.MONDAY - startDayOfWeek + 7) % 7;

            for (int w : weeks) {
                if ((isSingleWeek && w % 2 == 1) || (isDoubleWeek && w % 2 == 0) || (!isSingleWeek && !isDoubleWeek)) {
                    // 计算该周对应星期几的日期
                    calendar.setTime(startDate);
                    calendar.add(Calendar.DAY_OF_YEAR, (w - 1) * 7 + daysToFirstMonday + (dayOfWeek - Calendar.MONDAY));

                    // 获取开始和结束课程的索引（从 0 开始）
                    int startIndex = (startClass - 1) / 2;
                    int endIndex = (endClass - 1) / 2;

                    // 计算开始时间
                    String startStr = calendar.get(Calendar.YEAR) + "-" +
                            (calendar.get(Calendar.MONTH) + 1) + "-" +
                            calendar.get(Calendar.DAY_OF_MONTH) + " " +
                            CLASS_TIME_RANGES[startIndex][0];
                    Date startTime = dateFormat.parse(startStr);

                    // 计算结束时间
                    String endStr = calendar.get(Calendar.YEAR) + "-" +
                            (calendar.get(Calendar.MONTH) + 1) + "-" +
                            calendar.get(Calendar.DAY_OF_MONTH) + " " +
                            CLASS_TIME_RANGES[endIndex][1];
                    Date endTime = dateFormat.parse(endStr);

                    dateRanges.add(new DateRange(startTime, endTime));
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return dateRanges;
    }

    private static List<Integer> parseWeeks(String week) {
        List<Integer> weeks = new ArrayList<>();
        Pattern pattern = Pattern.compile("(\\d+)-(\\d+)|(\\d+)");
        Matcher matcher = pattern.matcher(week);
        while (matcher.find()) {
            if (matcher.group(1) != null && matcher.group(2) != null) {
                int start = Integer.parseInt(matcher.group(1));
                int end = Integer.parseInt(matcher.group(2));
                for (int i = start; i <= end; i++) {
                    weeks.add(i);
                }
            } else if (matcher.group(3) != null) {
                weeks.add(Integer.parseInt(matcher.group(3)));
            }
        }
        return weeks;
    }

    private static int[] parseClassPeriod(String period) {
        Pattern pattern = Pattern.compile("(.)\\[(\\d+)-(\\d+)节\\](单|双)?");
        Matcher matcher = pattern.matcher(period);
        if (matcher.find()) {
            int dayOfWeek = getDayOfWeek(matcher.group(1));
            int startClass = Integer.parseInt(matcher.group(2));
            int endClass = Integer.parseInt(matcher.group(3));
            String weekType = matcher.group(4);
            int isSingleWeek = weekType != null && weekType.equals("单") ? 1 : 0;
            int isDoubleWeek = weekType != null && weekType.equals("双") ? 1 : 0;
            return new int[]{dayOfWeek, startClass, endClass, isSingleWeek, isDoubleWeek};
        }
        return new int[0];
    }

    private static int getDayOfWeek(String day) {
        switch (day) {
            case "一":
                return Calendar.MONDAY;
            case "二":
                return Calendar.TUESDAY;
            case "三":
                return Calendar.WEDNESDAY;
            case "四":
                return Calendar.THURSDAY;
            case "五":
                return Calendar.FRIDAY;
            default:
                return -1;
        }
    }

    public static class DateRange {
        Date startTime;
        Date endTime;

        public DateRange(Date startTime, Date endTime) {
            this.startTime = startTime;
            this.endTime = endTime;
        }

        @Override
        public String toString() {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return sdf.format(startTime) + " - " + sdf.format(endTime);
        }
        public Date getStartTime() {
            return startTime;
        }

        public Date getEndTime() {
            return endTime;
        }


    }
}